<?php
namespace app\api\service;
use think\Controller;
use think\image\Exception;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/26 0026
 * Time: 15:40
 */

class NoteCode extends Controller {

    /**
     * 发送短信
     * @param $param
     * @return array
     */
    public static function send_code($param) {
        try {
            $code = rand(1000, 9999);
            $type = isset($param['type']) ? $param['type'] : 1;
            switch ($type) {
                case 1:
                    $msg = '【衣依不舍】您的验证码是' . $code;
                    break;
                case 2:
                    $msg = '【衣依不舍】发现新订单，请及时处理，订单号:' . $param['order_sn'] . '，定制人:' . $param['linkman'] . '，联系电话:' . $param['address_mobile'] . '，收货地址:' . $param['address'];
                    break;
                default :
                    throw new \Exception('短信类型错误！');
                    break;
            }

            $url = 'http://sapi.253.com/msg/HttpBatchSendSM?account=minnanws88&pswd=Mnws2016&mobile=' . $param['mobile'] . '&msg=' . $msg . '&needstatus=true';

            $data = file_get_contents($url);
            $data = explode(',', $data);
            $base_code = substr($data[1], 0, 1);
            if ($base_code > 0) {
                throw new Exception(get_note_error_msg($base_code));
            }

            // 发送成功  添加记录
            model('note_code')->save(['code' => $code, 'mobile' => $param['mobile']]);

            return api_return(200, []);
        } catch (\Exception $e) {
            return api_return(0, [], $e->getMessage());
        }
    }
}